package com.tinyseed.hmxx.admin.security.service.impl;

import com.tinyseed.hmxx.admin.security.domain.JwtUser;
import com.tinyseed.hmxx.admin.security.service.AuthService;
import com.tinyseed.hmxx.admin.security.utils.JwtTokenUtil;
import com.tinyseed.hmxx.admin.system.domain.User;
import com.tinyseed.hmxx.admin.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Date;

/**
 * @version Revision 1.0.0
 * @版权：
 * @author: Howard
 * @email:
 * @see:
 * @创建日期：2020/2/22
 * @功能说明：权限auth实现类
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2020/2/22 wuxiran        创建
 * @end
 */

public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private UserService userService;

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    /**
     * Description 注册并设置权限
     * Version 1.0
     * Date 2020/2/22 8:52 下午
     * Author howard
     *
     * @param userToAdd 添加的用户
     * @return 返回插入成功的用户
     */
    @Override
    public User register(User userToAdd) {
        final String useaname = userToAdd.getUsername();
        if (userService.findByName(useaname) != null) {
            return null;
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        final String rawPassword = userToAdd.getPassword();

        userToAdd.setPassword(encoder.encode(rawPassword));
        userToAdd.setLastPasswordResetDate(new Date());
        /*设置默认的用户权限*/
        //userToAdd.setRoles(new HashSet(("ROLE_USER")));
        return null;
    }

    /**
     * Description 登录返回token
     * Version 1.0
     * Date 2020/2/22 8:50 下午
     * Author howard
     *
     * @param username 用户名
     * @param password 密码
     * @return token 返回token
     */
    @Override
    public String login(String username, String password) {
        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(username, password);
        final Authentication authentication = authenticationManager.authenticate(upToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);

        final UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        return jwtTokenUtil.generateToken(userDetails);
    }
    /**
     * Description 刷新token
     * Version 1.0
     * Date 2020/2/22 8:52 下午
     * Author howard
     *
     * @param oldToken 旧token
     * @return
     */
    @Override
    public String refresh(String oldToken) {
        final String token = oldToken.substring(tokenHead.length());
        String username = jwtTokenUtil.getUsernameFromToken(token);
        JwtUser user = (JwtUser) userDetailsService.loadUserByUsername(username);
        if (jwtTokenUtil.canTokenBeRefreshed(token, user.getLastPasswordResetDate())) {
            return jwtTokenUtil.refreshToken(token);
        }
        return null;
    }
}
